package com.freelancer.musicinstruction.services.impl.youtube;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.freelancer.musicinstruction.services.impl.youtube.enums.ApiVersion;
import com.freelancer.musicinstruction.services.impl.youtube.enums.FeedFormat;
import com.freelancer.musicinstruction.services.impl.youtube.enums.OrderBy;
import com.freelancer.musicinstruction.services.impl.youtube.enums.PrettyPrint;
import com.freelancer.musicinstruction.services.impl.youtube.enums.Strict;
import com.freelancer.musicinstruction.services.impl.youtube.enums.VideoFormat;


public class YoutubeSearchModel {
	
	private Map<String, String> youtubeSearchParameters;
	
	
	/**
	 * Description of youtube data api can be seen here:
	 * http://code.google.com/apis/youtube/2.0/developers_guide_protocol_api_query_parameters.html
	 */
	private YoutubeSearchModel(){
		youtubeSearchParameters = new HashMap<String, String>();
		
		youtubeSearchParameters.put("alt", FeedFormat.RSS.getValue() );
		youtubeSearchParameters.put("prettyprint", PrettyPrint.ENABLE.getValue() );
		youtubeSearchParameters.put("max-results", "10");
		youtubeSearchParameters.put("start-index", "0");
		youtubeSearchParameters.put("format", VideoFormat.H263.getValue() );
		youtubeSearchParameters.put("v", ApiVersion.TWO.getValue());
		youtubeSearchParameters.put("orderby", OrderBy.RATING.getValue() );
		youtubeSearchParameters.put("strict", Strict.DISABLE.getValue() );
		youtubeSearchParameters.put("q", "video practice");
		
	}
	
	private static class YoutubeSearchModelSigleton {
	     private static final YoutubeSearchModel INSTANCE = new YoutubeSearchModel();
	}

	
	public static YoutubeSearchModel getInstance() {
	     return YoutubeSearchModelSigleton.INSTANCE;
	}

	public String getFeedType() {
		return youtubeSearchParameters.get("alt");
	}


	public void setFeedType(String feedType) {
		youtubeSearchParameters.put( "alt", feedType);
	}


	public String getPrettyPrint() {
		return youtubeSearchParameters.get("prettyprint");
	}


	public void setPrettyPrint(String prettyPrint) {
		youtubeSearchParameters.put( "prettyprint", prettyPrint);
	}


	public String getApiVersion() {
		return youtubeSearchParameters.get("v");
	}


	public void setApiVersion(String apiVersion) {
		youtubeSearchParameters.put( "v", apiVersion);
	}


	public String getStrict() {
		return youtubeSearchParameters.get("strict");
	}


	public void setStrict(String strict) {
		youtubeSearchParameters.put( "strict", strict);
	}


	public String getStartIndex() {
		return youtubeSearchParameters.get("start-index");
	}


	public void setStartIndex(String startIndex) {
		youtubeSearchParameters.put( "start-index", startIndex);
	}


	public String getMaxResults() {
		return youtubeSearchParameters.get("max-results");
	}


	public void setMaxResults(String maxResults) {
		youtubeSearchParameters.put( "max-results", maxResults);
	}


	public String getQuery() {
		return youtubeSearchParameters.get("q");
	}


	public void setQuery(String query) {
		youtubeSearchParameters.put( "q", query);
	}


	public String getOrderBy() {
		return youtubeSearchParameters.get("orderby");
	}


	public void setOrderBy(String orderBy) {
		youtubeSearchParameters.put( "orderby", orderBy);
	}


	public String getFormat() {
		return youtubeSearchParameters.get("format");
	}


	public void setFormat(String format) {
		youtubeSearchParameters.put( "format", format);
	}


	public Map<String, String> getYoutubeSearchParameters() {
		return youtubeSearchParameters;
	}


	public void setYoutubeSearchParameters(
			Map<String, String> youtubeSearchParameters) {
		this.youtubeSearchParameters = youtubeSearchParameters;
	}
	
	@SuppressWarnings("unchecked")
	public String generateQueryString(){
		StringBuilder sb = new StringBuilder("?");
		
		Iterator< Entry<String, String > > keyValuePairs = youtubeSearchParameters.entrySet().iterator();
		
		for( int i = 0; i < youtubeSearchParameters.size(); i++ ){
			Map.Entry entry = keyValuePairs.next();
			
			sb.append ( (String) entry.getKey() );
			sb.append ( "=" );
			sb.append ( (String) entry.getValue() );
			
			if( keyValuePairs.hasNext()){
				sb.append ( "&" );
			}
		}
		
		return sb.toString();
		
	}

}
